/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.deploy.rest;

import com.alien.admin.annotation.Log;
import com.alien.admin.modules.deploy.domain.DpServer;
import com.alien.admin.modules.deploy.service.DpServerService;
import com.alien.admin.modules.deploy.service.dto.DpServerDto;
import com.alien.admin.modules.deploy.service.dto.DpServerQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://el-admin.vip
* @author zengzhifang
* @date 2021-09-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "服务，也就是项目管理")
@RequestMapping("/api/dpServer")
public class DpServerController {

    private final DpServerService dpServerService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dpServer:list')")
    public void download(HttpServletResponse response, DpServerQueryCriteria criteria) throws IOException {
        dpServerService.download(dpServerService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询服务，也就是项目")
    @ApiOperation("查询服务，也就是项目")
    @PreAuthorize("@el.check('dpServer:list')")
    public ResponseEntity<Object> query(DpServerQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(dpServerService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/all")
    @Log("查询全部服务项目")
    @ApiOperation("查询全部服务项目")
    @PreAuthorize("@el.check('dpServer:list')")
    public ResponseEntity<Object> queryAll(DpServerQueryCriteria criteria){
        return new ResponseEntity<>(dpServerService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping(value = "/detail")
    @Log("查询单个服务，也就是项目")
    @ApiOperation("查询单个服务，也就是项目")
    @PreAuthorize("@el.check('dpServer:list')")
    public ResponseEntity<Object> detail(DpServerQueryCriteria criteria){
        DpServerDto dpServerDto = dpServerService.findById(criteria.getProjectId());
        return new ResponseEntity<>(dpServerDto,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增服务，也就是项目")
    @ApiOperation("新增服务，也就是项目")
    @PreAuthorize("@el.check('dpServer:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DpServer resources){
        return new ResponseEntity<>(dpServerService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改服务，也就是项目")
    @ApiOperation("修改服务，也就是项目")
    @PreAuthorize("@el.check('dpServer:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DpServer resources){
        dpServerService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除服务，也就是项目")
    @ApiOperation("删除服务，也就是项目")
    @PreAuthorize("@el.check('dpServer:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        dpServerService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
